package com.kurumi.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.client.solrj.response.FieldStatsInfo;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kurumi.config.MyConfig;
import com.kurumi.enums.EnumQuery;
import com.kurumi.pojo.MedicalRecord;
import com.kurumi.query.InPatientMedicalRecordItem;
import com.kurumi.query.InPatientMedicalRecordQuery;
import com.kurumi.query.StatisticsAnalysisQuery;
import com.kurumi.service.SolrService;
import com.kurumi.solr.MedicalRecordSolr;
import com.kurumi.util.DateUtil;
import com.kurumi.util.JsonUtil;
import com.kurumi.util.NumberUtils;
import com.kurumi.util.ObjectUtils;
@Service
public class SolrServiceImpl implements SolrService {

	@Autowired
	private MyConfig myConfig;

	/*private String serverUrl = "http://localhost:8080/solr/core1";*/
	private String solrDateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";

	@Override
	public int upadteIndex(String visitGuid,Map<String, Object> jsonMap){
		// TODO Auto-generated method stub
		HttpSolrClient client = new  HttpSolrClient(myConfig.getSolrCorePath());
	    SolrInputDocument doc = new SolrInputDocument();
	    Map<String, Object> basicInfo = (Map<String, Object>)jsonMap.get("basicInfo");
	    doc.addField("id", visitGuid);
	    String basicInfoJson = JsonUtil.objectToJsonIgnoreNull(basicInfo);
	    basicInfoJson = basicInfoJson.replaceAll("\"", "");
	    doc.addField("basic_info", basicInfoJson);
	    try {
			client.add(doc);
			UpdateResponse response = client.commit();
			System.err.println(response.getStatus());
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				client.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return 0;
	}

	private void addQueryCombination(StringBuilder params,String queryCombination){
		if(params.length() > 0){
			if(queryCombination.equalsIgnoreCase("OR")){
				params.append(")");
				params.append(" "+queryCombination+" ");
				params.append("(");
			}else{
				params.append(" "+queryCombination+" ");
			}

		}else{
			params.append("(");
		}
	}

	@Override
	public List<MedicalRecordSolr> query(InPatientMedicalRecordQuery query) {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		List<MedicalRecordSolr> medicalRecordSolrs = new ArrayList<MedicalRecordSolr>();
		HttpSolrClient client = null;

		try {
			client = new  HttpSolrClient(myConfig.getSolrCorePath());
			List<InPatientMedicalRecordItem> items = query.getItems();
			SolrQuery solrQuery = new SolrQuery();
			StringBuilder params = new StringBuilder();

			for (InPatientMedicalRecordItem item : items) {
				if(item.getQueryCode().startsWith("str_") || item.getQueryCode().startsWith("array_") ) {
					if(item.getQueryCondition().equals("=")){
						String q =item.getQueryCode()+":"+item.getQueryValue();
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("!=")){
						String q ="-"+item.getQueryCode()+":"+item.getQueryValue();
/*						if(params.length() > 0){
							params.append(" "+item.getQueryCombination()+" ");
							params.append(q);
						}else{
							params.append(q);
						}*/
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals(">=")){
						String q = item.getQueryCode()+":["+item.getQueryValue()+" TO *]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals(">")){
						String q = item.getQueryCode()+":["+item.getQueryValue()+"! TO *]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}
					else if(item.getQueryCondition().equals("<=")){
						String q = item.getQueryCode()+":[* TO "+item.getQueryValue()+"]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("<")){
						String q = item.getQueryCode()+":[* TO "+item.getQueryValue()+"~]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("contains")){
						String q =item.getQueryCode()+":*"+item.getQueryValue()+"*";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("uncontains")){
						String q ="-"+item.getQueryCode()+":*"+item.getQueryValue()+"*";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("prefix")){
						String q =item.getQueryCode()+":"+item.getQueryValue()+"*";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("suffix")){
						String q =item.getQueryCode()+":*"+item.getQueryValue();
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}
				}else if(item.getQueryCode().startsWith("date_")) {
					if(item.getQueryCondition().equals("=") || item.getQueryCondition().equals("contains")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							Date endDate = DateUtil.getNextDay(startDate);
							String q = item.getQueryCode()+":["+DateUtil.dateFormat(solrDateFormat, startDate)+" TO "+DateUtil.dateFormat(solrDateFormat, endDate)+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals(">=")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":["+DateUtil.dateFormat(solrDateFormat, startDate)+" TO *]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals(">")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":["+DateUtil.dateFormat(solrDateFormat, startDate)+" TO *]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else if(item.getQueryCondition().equals("<=")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":[* TO "+DateUtil.dateFormat(solrDateFormat, DateUtil.getNextDay(startDate))+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals("<")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":[* TO "+DateUtil.dateFormat(solrDateFormat, DateUtil.getNextDay(startDate))+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}else if(item.getQueryCode().startsWith("int_") || item.getQueryCode().startsWith("float_")) {

					if(item.getQueryCondition().equals("=") || item.getQueryCondition().equals("contains")){

						String q = item.getQueryCode()+":["+item.getQueryValue()+" TO "+item.getQueryValue()+"]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals(">=")){
						String q = item.getQueryCode()+":["+item.getQueryValue()+" TO *]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals(">")){
						try {
							Float number = Float.parseFloat(item.getQueryValue())+1;
							String q = item.getQueryCode()+":["+number+" TO *]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals("<=")){
						try {
							Float number = Float.parseFloat(item.getQueryValue())+1;
							String q = item.getQueryCode()+":[* TO "+number+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals("<")){
						try {
							Float number =Float.parseFloat(item.getQueryValue())+1;
							String q = item.getQueryCode()+":[* TO "+number+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}


				}
			}
			params.append(")");
			solrQuery.setQuery(params.toString());
			StringBuilder queryField = new StringBuilder();
			queryField.append("visit_guid");
			queryField.append(","+EnumQuery.str_patient_name);
			queryField.append(","+EnumQuery.str_only_id);
			queryField.append(","+EnumQuery.str_mr_id);
			queryField.append(","+EnumQuery.int_visit_number);
			queryField.append(","+EnumQuery.date_birthday);
			queryField.append(","+EnumQuery.str_id_number);
			queryField.append(","+EnumQuery.str_out_dept);
			queryField.append(","+EnumQuery.date_out_hospital_date);
			queryField.append(","+EnumQuery.str_out_hospital_type);
			queryField.append(","+EnumQuery.float_in_hospital_cost);
			queryField.append(","+EnumQuery.float_blood_cost);
			queryField.append(","+EnumQuery.float_surgery_cost);
			queryField.append(","+EnumQuery.float_in_hospital_day_number);
			solrQuery.setFields(queryField.toString());
			solrQuery.setRows(query.getPageSize());
			solrQuery.setStart(query.getCurrentStartNum());
			QueryResponse queryResponse = client.query(solrQuery);
			SolrDocumentList results = queryResponse.getResults();
			query.setTotalCounts((int)results.getNumFound());
			for (SolrDocument solrDoc : results) {
				Object visitGuid = solrDoc.get("visit_guid");
				Object patientName = solrDoc.get(EnumQuery.str_patient_name.getCode());
				Object onlyId = solrDoc.get(EnumQuery.str_only_id.getCode());
				Object mrId = solrDoc.get(EnumQuery.str_mr_id.getCode());
				Object visitNumber = solrDoc.get(EnumQuery.int_visit_number.getCode());
				Object idNumber = solrDoc.get(EnumQuery.str_id_number.getCode());
				Object outDeptName = solrDoc.get(EnumQuery.str_out_dept.getCode());
				Object outHospitalTypeName = solrDoc.get(EnumQuery.str_out_hospital_type.getCode());
				Object birthday = solrDoc.get(EnumQuery.date_birthday.getCode());
				Object outHospitalDateTime = solrDoc.get(EnumQuery.date_out_hospital_date.getCode());
				Object inHospitalCost = solrDoc.get(EnumQuery.float_in_hospital_cost.getCode());
				Object bloodCost = solrDoc.get(EnumQuery.float_blood_cost.getCode());
				Object surgeryCost = solrDoc.get(EnumQuery.float_surgery_cost.getCode());
				Object inHospitalDayNumber = solrDoc.get(EnumQuery.float_in_hospital_day_number.getCode());

				MedicalRecordSolr medicalRecordSolr = new MedicalRecordSolr();
				medicalRecordSolr.setVisitGuid(ObjectUtils.getStringValue(visitGuid));
				medicalRecordSolr.setOnlyId(ObjectUtils.getStringValue(onlyId));
				medicalRecordSolr.setMrId(ObjectUtils.getStringValue(mrId));
				medicalRecordSolr.setVisitNumber(ObjectUtils.getIntegerValue(visitNumber));
				medicalRecordSolr.setIdNumber(ObjectUtils.getStringValue(idNumber));

				medicalRecordSolr.setPatientName(ObjectUtils.getStringValue(patientName));

				medicalRecordSolr.setBirthday(ObjectUtils.getDateTimeValue(birthday));
				medicalRecordSolr.setOutDeptName(ObjectUtils.getStringValue(outDeptName));
				medicalRecordSolr.setOutHospitalTypeName(ObjectUtils.getStringValue(outHospitalTypeName));
				medicalRecordSolr.setBirthday(ObjectUtils.getDateValue(birthday));
				medicalRecordSolr.setOutHospitalDateTime(ObjectUtils.getGTCDeteTimeValue(outHospitalDateTime));
				medicalRecordSolr.setInHospitalDayNumber(ObjectUtils.getIntegerValue(inHospitalDayNumber));
				medicalRecordSolr.setTotalCostMoney(ObjectUtils.getBigDecimalValue(inHospitalCost));
				medicalRecordSolr.setBloodCost(ObjectUtils.getBigDecimalValue(bloodCost));
				medicalRecordSolr.setSurgeryCost(ObjectUtils.getBigDecimalValue(surgeryCost));
				medicalRecordSolrs.add(medicalRecordSolr);
			}
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(client!= null){
					client.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return medicalRecordSolrs;

	}
	@Override
	public List<Map<String,Object>> diagTop10Facet(StatisticsAnalysisQuery params) {
		// TODO Auto-generated method stub
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		HttpSolrClient client = null;
		try{
			client = new  HttpSolrClient(myConfig.getSolrCorePath());
			SolrQuery solrQuery = new SolrQuery();

			solrQuery.setFacet(true);// 设置facet=on
			solrQuery.addFacetField(new String[] {"str_hqms_main_diag"});// 设置需要facet的字段
			int rowCount = 10;
			if(params.getTopNumber()!= null){
				try{
					rowCount = Integer.parseInt(params.getTopNumber());
				}catch (Exception e) {
					// TODO: handle exception
				}

			}
			solrQuery.setFacetLimit(rowCount);// 限制facet返回的数量
	        solrQuery.setFacetMissing(false);// 不统计null的值
	        solrQuery.setFacetMinCount(0);// 设置返回的数据中每个分组的数据最小值，比如设置为1，则统计数量最小为1，不然不显示
	        solrQuery.setFacetSort("count");

	        if(params.getOutHospitalStartDate() == null || params.getOutHospitalEndDate() == null){
	        	solrQuery.set("q", "*:*" );
	        }else{
				 String q = EnumQuery.date_out_hospital_date+":["+DateUtil.dateFormat(solrDateFormat, params.getOutHospitalStartDate())+" TO "+DateUtil.dateFormat(solrDateFormat, params.getOutHospitalEndDate())+"]";
	        	 solrQuery.setQuery(q);
	        }
	        QueryResponse queryResponse = client.query(solrQuery);
			List<FacetField> facets = queryResponse.getFacetFields();//返回的facet列表

			for (FacetField facet : facets) {
			    List<Count> counts = facet.getValues();
			    for (Count count : counts) {
			    	Map<String,Object> data = new HashMap<String, Object>();
			    	data.put("main_diag_desc", count.getName());
			    	data.put("diag_count", count.getCount());
			    	datas.add(data);
			        System.out.println(count.getName() + ":" + count.getCount());

			    }

			    System.out.println();

			}

		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return datas;


	}
	@Override
	public List<Map<String, Object>> operateTop10Facet(StatisticsAnalysisQuery params) {
		// TODO Auto-generated method stub
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		HttpSolrClient client = null;
		try{
			client = new  HttpSolrClient(myConfig.getSolrCorePath());
			SolrQuery solrQuery = new SolrQuery();

			solrQuery.setFacet(true);// 设置facet=on
			solrQuery.addFacetField(new String[] {"str_hqms_main_operate"});// 设置需要facet的字段
			int rowCount = 10;
			if(params.getTopNumber()!= null){
				try{
					rowCount = Integer.parseInt(params.getTopNumber());
				}catch (Exception e) {
					// TODO: handle exception
				}

			}
			solrQuery.setFacetLimit(rowCount);// 限制facet返回的数量
	        solrQuery.setFacetMissing(false);// 不统计null的值
	        solrQuery.setFacetMinCount(0);// 设置返回的数据中每个分组的数据最小值，比如设置为1，则统计数量最小为1，不然不显示
	       	solrQuery.setFacetSort("count");

	        if(params.getOutHospitalStartDate() == null || params.getOutHospitalEndDate() == null){
	        	solrQuery.set("q", "*:*" );
	        }else{
				 String q = EnumQuery.date_out_hospital_date+":["+DateUtil.dateFormat(solrDateFormat, params.getOutHospitalStartDate())+" TO "+DateUtil.dateFormat(solrDateFormat, params.getOutHospitalEndDate())+"]";
	        	 solrQuery.setQuery(q);
	        }
	        QueryResponse queryResponse = client.query(solrQuery);
			List<FacetField> facets = queryResponse.getFacetFields();//返回的facet列表

			for (FacetField facet : facets) {
			    List<Count> counts = facet.getValues();
			    for (Count count : counts) {
			    	Map<String,Object> data = new HashMap<String, Object>();
			    	data.put("main_operate_desc", count.getName());
			    	data.put("operate_count", count.getCount());
			    	datas.add(data);
			        System.out.println(count.getName() + ":" + count.getCount());

			    }

			    System.out.println();

			}

		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return datas;
	}
	private String getUnplanReHospitalQuery(Date outHospitalStartDate,Date outHospitalEndDate){
		StringBuilder query = new StringBuilder();
		query.append(EnumQuery.date_out_hospital_date+":["+DateUtil.dateFormat(solrDateFormat, outHospitalStartDate)+" TO "+DateUtil.dateFormat(solrDateFormat, outHospitalEndDate)+"]");
		query.append(" AND ");
		query.append(EnumQuery.str_5.getCode()+":是");

		return query.toString();
	}

	private String getUnplanReOperateQuery(Date outHospitalStartDate,Date outHospitalEndDate){
		StringBuilder query = new StringBuilder();
		query.append(EnumQuery.date_out_hospital_date+":["+DateUtil.dateFormat(solrDateFormat, outHospitalStartDate)+" TO "+DateUtil.dateFormat(solrDateFormat, outHospitalEndDate)+"]");
		query.append(" AND ");
		query.append(EnumQuery.str_hqms_is_un_planed_resurgery.getCode()+":1");

		return query.toString();
	}

	private String getBaseKeyDiseasesQuery(String keyDiseasesCode,Date outHospitalStartDate,Date outHospitalEndDate){
		StringBuilder query = new StringBuilder();
		query.append(EnumQuery.date_out_hospital_date+":["+DateUtil.dateFormat(solrDateFormat, outHospitalStartDate)+" TO "+DateUtil.dateFormat(solrDateFormat, outHospitalEndDate)+"]");
		query.append(" AND ");
		query.append(EnumQuery.int_age_year.getCode()+":[18 TO *]");
		if(keyDiseasesCode.equalsIgnoreCase("1")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[I21! TO I22~]");
		}else if(keyDiseasesCode.equalsIgnoreCase("2")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[I05! TO I09~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[I11! TO I13~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[I20! TO I25~]");
			query.append(" ) ");
			query.append(" AND ");
			query.append(EnumQuery.array_hqms_other_diag_codes.getCode()+":I50*");
		}else if(keyDiseasesCode.equalsIgnoreCase("3")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[I60! TO I63~]");
		}else if(keyDiseasesCode.equalsIgnoreCase("4")){
			//query.append(" AND ");
			//query.append(EnumQuery.str_main_diag_code.getCode()+":S60*");
			query =  new StringBuilder();
			query.append("(int_age_year:[18 TO *] AND str_hqms_main_diag_code:*S06* AND date_out_hospital_date:["+DateUtil.dateFormat(solrDateFormat, outHospitalStartDate)+" TO *] AND date_out_hospital_date:[* TO "+DateUtil.dateFormat(solrDateFormat, outHospitalEndDate)+"])");


		}else if(keyDiseasesCode.equalsIgnoreCase("5")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K25.0*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K25.2*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K25.4*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K25.6*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K26.0*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K26.2*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K26.4*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K26.6*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K27.0*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K27.2*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K27.4*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K27.6*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K28.0*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K28.2*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K28.4*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K28.6*");
			query.append(" ) ");
		}else if(keyDiseasesCode.equalsIgnoreCase("6")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[T00! TO T07~]");
		}else if(keyDiseasesCode.equalsIgnoreCase("7")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":J10.0*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":J11.0*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[J12! TO J18~]");
			query.append(" ) ");
			query.append(" AND ");
			query.append("-"+EnumQuery.str_hqms_main_diag_code.getCode()+":J17*");
		}else if(keyDiseasesCode.equalsIgnoreCase("8")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":J44.0*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":J44.1*");
			query.append(" ) ");
		}else if(keyDiseasesCode.equalsIgnoreCase("9")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[E10! TO E14~]");
			query.append(" OR ");
			query.append(EnumQuery.array_hqms_other_operate_codes.getCode()+":84.1*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":84.1*");
			query.append(" ) ");
		}else if(keyDiseasesCode.equalsIgnoreCase("10")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":E04*");
			query.append(" ) ");
			/*query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_main_diag_code.getCode()+":E04.1*");
			query.append(" OR ");
			query.append(EnumQuery.str_main_diag_code.getCode()+":E04.2*");
			query.append(" ) ");*/
		}else if(keyDiseasesCode.equalsIgnoreCase("11")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K35.0*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K35.1*");
			query.append(" ) ");
		}else if(keyDiseasesCode.equalsIgnoreCase("12")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":N40*");
		}else if(keyDiseasesCode.equalsIgnoreCase("13")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[N17! TO N19~]");
		}else if(keyDiseasesCode.equalsIgnoreCase("14")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[A40! TO A41~]");
		}else if(keyDiseasesCode.equalsIgnoreCase("15")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[I10! TO I15~]");
		}else if(keyDiseasesCode.equalsIgnoreCase("16")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":K85*");
		}else if(keyDiseasesCode.equalsIgnoreCase("17")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":Z51.102*");
		}else if(keyDiseasesCode.equalsIgnoreCase("18")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":Z51.103*");
		}

		return query.toString();
	}

	@Override
	public List<Map<String, Object>> eighteenKeyDiseases(StatisticsAnalysisQuery params) throws SolrServerException, IOException {
		// TODO Auto-generated method stub
		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		HttpSolrClient client = null;
		String baseKeyDiseasesQuery = getBaseKeyDiseasesQuery(params.getKeyDiseasesCode(),params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		client = new  HttpSolrClient(myConfig.getSolrCorePath());
		SolrQuery solrQuery = new SolrQuery();
		StringBuilder query = new StringBuilder();
		query.append(baseKeyDiseasesQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = client.query(solrQuery);
		SolrDocumentList results = queryResponse.getResults();
		int diseasesCount = (int)results.getNumFound();
		data.put("diseases_count",diseasesCount);
		query = new StringBuilder();
		query.append(baseKeyDiseasesQuery);
		query.append(" AND ");
		query.append(EnumQuery.str_out_hospital_type+":死亡");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		data.put("death_count", (int)results.getNumFound());
		query = new StringBuilder();
		query.append(baseKeyDiseasesQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_last_in_hospital_interval.getCode()+":[0 TO 15]");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate15Count = (int)results.getNumFound();
		data.put("rate_15_count",rate15Count);
		data.put("rate_15", NumberUtils.getPrecent(new BigDecimal(rate15Count), new BigDecimal(diseasesCount))+"%");
		query = new StringBuilder();
		query.append(baseKeyDiseasesQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_last_in_hospital_interval.getCode()+":[0 TO 30]");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate30Count = (int)results.getNumFound();
		data.put("rate_31_count",rate30Count);
		data.put("rate_31", NumberUtils.getPrecent(new BigDecimal(rate30Count), new BigDecimal(diseasesCount))+"%");
		query = new StringBuilder();
		query.append(baseKeyDiseasesQuery);

		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		solrQuery.setGetFieldStatistics(true);
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_day_number.getCode());
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_cost.getCode());
		queryResponse = client.query(solrQuery);
		Map<String,FieldStatsInfo> fieldStatsInfos = queryResponse.getFieldStatsInfo();
		if(fieldStatsInfos != null){
			for (String fieldKey : fieldStatsInfos.keySet()) {
				if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_cost.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalCost = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());
					if(avgInHospitalCost!=null){
						data.put("avg_in_hospital_cost",NumberUtils.getString(avgInHospitalCost, 2));
					}


				}else if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_day_number.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalDay = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());
					if(avgInHospitalDay!=null){
						data.put("avg_in_hospital_day",NumberUtils.getString(avgInHospitalDay, 2));
					}


				}
			}
		}
		datas.add(data);
		return datas;
	}

	@Override
	public List<Map<String, Object>> unplanReHospital(StatisticsAnalysisQuery params) throws SolrServerException, IOException {
		// TODO Auto-generated method stub
		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		HttpSolrClient client = null;
		String unplanReHospitalQuery = getUnplanReHospitalQuery(params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		client = new  HttpSolrClient(myConfig.getSolrCorePath());
		SolrQuery solrQuery = new SolrQuery();
		StringBuilder query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = client.query(solrQuery);
		SolrDocumentList results = queryResponse.getResults();
		int diseasesCount = (int)results.getNumFound();
		data.put("diseases_count",diseasesCount);//非计划再住院总病例数量

		query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_last_in_hospital_interval.getCode()+":0");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate0Count = (int)results.getNumFound();
		data.put("rate_0_count",rate0Count);//15日在住院数
		data.put("rate_0", NumberUtils.getPrecent(new BigDecimal(rate0Count), new BigDecimal(diseasesCount))+"%");//当日在住院率

		query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_last_in_hospital_interval.getCode()+":2");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate15Count = (int)results.getNumFound();
		data.put("rate_15_count",rate15Count);//15日在住院数
		data.put("rate_15", NumberUtils.getPrecent(new BigDecimal(rate15Count), new BigDecimal(diseasesCount))+"%");//15日在住院率
		query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_last_in_hospital_interval.getCode()+":16");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate31Count = (int)results.getNumFound();
		data.put("rate_31_count",rate31Count);//31日再住院数量
		data.put("rate_31", NumberUtils.getPrecent(new BigDecimal(rate31Count), new BigDecimal(diseasesCount))+"%");//31日再住院率

		query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_last_in_hospital_interval.getCode()+":32");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate32Count = (int)results.getNumFound();
		data.put("rate_more_count",rate32Count);//31日再住院数量
		data.put("rate_more", NumberUtils.getPrecent(new BigDecimal(rate32Count), new BigDecimal(diseasesCount))+"%");//32日及以上时长再住院率

		query = new StringBuilder();
		query.append(unplanReHospitalQuery);

		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		solrQuery.setGetFieldStatistics(true);
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_day_number.getCode());
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_cost.getCode());
		queryResponse = client.query(solrQuery);
		Map<String,FieldStatsInfo> fieldStatsInfos = queryResponse.getFieldStatsInfo();
		if(fieldStatsInfos != null){
			for (String fieldKey : fieldStatsInfos.keySet()) {
				if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_cost.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalCost = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());
					if(avgInHospitalCost!=null){
						data.put("avg_in_hospital_cost",NumberUtils.getString(avgInHospitalCost, 2));//住院平均费用
					}


				}else if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_day_number.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalDay = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());
					if(avgInHospitalDay!=null){
						data.put("avg_in_hospital_day",NumberUtils.getString(avgInHospitalDay, 2));//住院平均时长
					}
				}
			}
		}
		datas.add(data);
		return datas;
	}

	@Override
	public List<Map<String, Object>> unplanReOperate(StatisticsAnalysisQuery params) throws SolrServerException, IOException {
		// TODO Auto-generated method stub
		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		HttpSolrClient client = null;
		String unplanReHospitalQuery = getUnplanReOperateQuery(params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		client = new  HttpSolrClient(myConfig.getSolrCorePath());
		SolrQuery solrQuery = new SolrQuery();
		StringBuilder query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = client.query(solrQuery);
		SolrDocumentList results = queryResponse.getResults();
		int diseasesCount = (int)results.getNumFound();
		data.put("diseases_count",diseasesCount);//非计划再住院总病例数量

		query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode()+":0");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate0Count = (int)results.getNumFound();
		data.put("rate_0_count",rate0Count);//15日在住院数
		data.put("rate_0", NumberUtils.getPrecent(new BigDecimal(rate0Count), new BigDecimal(diseasesCount))+"%");//当日在住院率

		query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode()+":2");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate15Count = (int)results.getNumFound();
		data.put("rate_15_count",rate15Count);//15日在住院数
		data.put("rate_15", NumberUtils.getPrecent(new BigDecimal(rate15Count), new BigDecimal(diseasesCount))+"%");//15日在住院率
		query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode()+":16");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate31Count = (int)results.getNumFound();
		data.put("rate_31_count",rate31Count);//31日再住院数量
		data.put("rate_31", NumberUtils.getPrecent(new BigDecimal(rate31Count), new BigDecimal(diseasesCount))+"%");//31日再住院率

		query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode()+":32");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate32Count = (int)results.getNumFound();
		data.put("rate_more_count",rate32Count);//31日再住院数量
		data.put("rate_more", NumberUtils.getPrecent(new BigDecimal(rate32Count), new BigDecimal(diseasesCount))+"%");//32日及以上时长再住院率

		query = new StringBuilder();
		query.append(unplanReHospitalQuery);

		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		solrQuery.setGetFieldStatistics(true);
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_day_number.getCode());
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_cost.getCode());
		queryResponse = client.query(solrQuery);
		Map<String,FieldStatsInfo> fieldStatsInfos = queryResponse.getFieldStatsInfo();
		if(fieldStatsInfos != null){
			for (String fieldKey : fieldStatsInfos.keySet()) {
				if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_cost.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalCost = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());
					if(avgInHospitalCost!=null){
						data.put("avg_in_hospital_cost",NumberUtils.getString(avgInHospitalCost, 2));//住院平均费用
					}


				}else if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_day_number.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalDay = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());
					if(avgInHospitalDay!=null){
						data.put("avg_in_hospital_day",NumberUtils.getString(avgInHospitalDay, 2));//住院平均时长
					}
				}
			}
		}
		datas.add(data);
		return datas;
	}



	private String getBaseKeySurgeryQuery(String keySurgeryCode,Date outHospitalStartDate,Date outHospitalEndDate){
		StringBuilder query = new StringBuilder();
		query.append(EnumQuery.date_out_hospital_date+":["+DateUtil.dateFormat(solrDateFormat, outHospitalStartDate)+" TO "+DateUtil.dateFormat(solrDateFormat, outHospitalEndDate)+"]");
		query.append(" AND ");
		query.append(EnumQuery.int_age_year.getCode()+":[18 TO *]");
		if(keySurgeryCode.equalsIgnoreCase("1")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[06.2! TO 06.5~]");
		}else if(keySurgeryCode.equalsIgnoreCase("2")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":80.6*");
		}else if(keySurgeryCode.equalsIgnoreCase("3")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[68.3! TO 68.7~]");
		}else if(keySurgeryCode.equalsIgnoreCase("4")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[74.0! TO 74.2~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":74.4*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":74.9*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("5")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[53.0! TO 53.1~]");
		}else if(keySurgeryCode.equalsIgnoreCase("6")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":47.0*");
		}else if(keySurgeryCode.equalsIgnoreCase("7")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":85.4*");
		}
		return query.toString();
	}
	@Override
	public List<Map<String, Object>> sevenKeySurgery(StatisticsAnalysisQuery params)
			throws SolrServerException, IOException {
		// TODO Auto-generated method stub
		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		HttpSolrClient client = null;
		String baseKeySurgeryQuery = getBaseKeySurgeryQuery(params.getKeySurgeryCode(),params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		client = new  HttpSolrClient(myConfig.getSolrCorePath());
		SolrQuery solrQuery = new SolrQuery();
		StringBuilder query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = client.query(solrQuery);
		SolrDocumentList results = queryResponse.getResults();
		int surgeryCount = (int)results.getNumFound();
		data.put("surgery_count",surgeryCount);
		query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		query.append(" AND ");
		query.append(EnumQuery.str_out_hospital_type+":死亡");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		data.put("death_count", (int)results.getNumFound());
		query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode()+":[0 TO 15]");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate15Count = (int)results.getNumFound();
		data.put("count_15", rate15Count);
		data.put("rate_15", NumberUtils.getPrecent(new BigDecimal(rate15Count), new BigDecimal(surgeryCount))+"%");

		query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		solrQuery.setGetFieldStatistics(true);
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_day_number.getCode());
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_cost.getCode());
		queryResponse = client.query(solrQuery);
		Map<String,FieldStatsInfo> fieldStatsInfos = queryResponse.getFieldStatsInfo();
		if(fieldStatsInfos != null){
			for (String fieldKey : fieldStatsInfos.keySet()) {
				if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_cost.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalCost = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());

					data.put("avg_in_hospital_cost",avgInHospitalCost==null?null:NumberUtils.getString(avgInHospitalCost, 2));

				}else if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_day_number.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalDay = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());

					data.put("avg_in_hospital_day",avgInHospitalDay==null?null:NumberUtils.getString(avgInHospitalDay, 2));

				}
			}
		}
		datas.add(data);
		return datas;
	}

	@Override
	public List<Map<String, String>> eighteenKeyDiseasesForExport(StatisticsAnalysisQuery params)
			throws SolrServerException, IOException {
		// TODO Auto-generated method stub

		List<Map<String,String>> datas = new ArrayList<Map<String,String>>();
		String baseKeyDiseasesQuery = getBaseKeyDiseasesQuery(params.getKeyDiseasesCode(),params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		HttpSolrClient client = new  HttpSolrClient(myConfig.getSolrCorePath());
		StringBuilder queryField = new StringBuilder();
		queryField.append("visit_guid");
		queryField.append(","+EnumQuery.str_only_id);
		queryField.append(","+EnumQuery.str_patient_name);
		queryField.append(","+EnumQuery.date_out_hospital_date);
		queryField.append(","+EnumQuery.str_out_dept);
		queryField.append(","+EnumQuery.str_out_hospital_type);
		queryField.append(","+EnumQuery.float_in_hospital_day_number);
		queryField.append(","+EnumQuery.str_hqms_main_diag_code);
		queryField.append(","+EnumQuery.str_hqms_main_diag);
		queryField.append(","+EnumQuery.array_hqms_other_diag);
		queryField.append(","+EnumQuery.float_in_hospital_cost);
		queryField.append(","+EnumQuery.int_last_in_hospital_interval);
		SolrQuery solrQuery = new SolrQuery();
		solrQuery.setFields(queryField.toString());
		StringBuilder query = new StringBuilder();
		query.append(baseKeyDiseasesQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = null;
		SolrDocumentList results = null;
		int totalCount = 0;
		int readCount = 0;
		while(true){
			solrQuery.setStart(readCount);
			readCount +=100;
			solrQuery.setRows(100);
			queryResponse = client.query(solrQuery);
			results = queryResponse.getResults();
			for (SolrDocument solrDoc : results) {
				Object onlyId = solrDoc.get(EnumQuery.str_only_id.getCode());
				Object patientName = solrDoc.get(EnumQuery.str_patient_name.getCode());
				Object outHospitalDateTime = solrDoc.get(EnumQuery.date_out_hospital_date.getCode());
				Object outDeptName = solrDoc.get(EnumQuery.str_out_dept.getCode());
				String outHospitalTypeName =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_out_hospital_type.getCode()));
				String inHospitalDayNumber =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_in_hospital_day_number.getCode()));
				String mainDiagCode =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_diag_code.getCode()));
				String mainDiag =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_diag.getCode()));
				Object totalCost =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_in_hospital_cost.getCode()));
				Object otherDiags =  solrDoc.get(EnumQuery.array_hqms_other_diag.getCode());
				Object reHospitalIntervalObj = solrDoc.get(EnumQuery.int_last_in_hospital_interval.getCode());
				Map<String, String> data = new HashMap<String, String>();
				data.put("only_id", ObjectUtils.getStringValue(onlyId));
				data.put("patient_name", ObjectUtils.getStringValue(patientName));
				data.put("out_hospital_date", ObjectUtils.getStringValue(outHospitalDateTime));
				data.put("out_dept_name", ObjectUtils.getStringValue(outDeptName));
				if(outHospitalTypeName.contains("死")){
					data.put("is_death", "是");
				}else{
					data.put("is_death", "否");
				}
				data.put("in_hospital_day_number", ObjectUtils.getStringValue(inHospitalDayNumber));
				data.put("is_15_rehospital", "否");
				data.put("is_31_rehospital", "否");
				if(reHospitalIntervalObj != null){
					int reHospitalInterval = ObjectUtils.getIntegerValue(reHospitalIntervalObj);
					if(reHospitalInterval<31){
						data.put("is_31_rehospital", "是");
					}
					if(reHospitalInterval<16){
						data.put("is_15_rehospital", "是");
					}
				}
				data.put("main_diag_code", mainDiagCode);
				data.put("main_diag", mainDiag);
				if(otherDiags != null){
					data.put("other_diags", String.join(";", (List)otherDiags));
				}
				data.put("total_cost", ObjectUtils.getStringValue(totalCost));
				datas.add(data);
			}
			totalCount = (int)results.getNumFound();
			if(readCount >= totalCount){
				break;
			}

		}



		return datas;
	}

	@Override
	public List<Map<String, String>> unplanReHospitalExportExcel(StatisticsAnalysisQuery params)
			throws SolrServerException, IOException {
		// TODO Auto-generated method stub

		List<Map<String,String>> datas = new ArrayList<Map<String,String>>();
		String unplanReHospitalQuery = getUnplanReHospitalQuery(params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		HttpSolrClient client = new  HttpSolrClient(myConfig.getSolrCorePath());
		StringBuilder queryField = new StringBuilder();
		queryField.append("visit_guid");
		queryField.append(","+EnumQuery.str_only_id);
		queryField.append(","+EnumQuery.str_patient_name);
		queryField.append(","+EnumQuery.date_out_hospital_date);
		queryField.append(","+EnumQuery.str_out_hospital_type);
		queryField.append(","+EnumQuery.float_cost1);
		queryField.append(","+EnumQuery.float_cost22);
		queryField.append(","+EnumQuery.float_cost13);
		queryField.append(","+EnumQuery.float_in_hospital_day_number);
		queryField.append(","+EnumQuery.str_5);
		queryField.append(","+EnumQuery.int_last_in_hospital_interval);
		queryField.append(","+EnumQuery.str_5_diagname.getCode());
		queryField.append(","+EnumQuery.str_hqms_main_diag_code);
		queryField.append(","+EnumQuery.str_hqms_main_diag);
		queryField.append(","+EnumQuery.str_5_diagname);

		SolrQuery solrQuery = new SolrQuery();
		solrQuery.setFields(queryField.toString());
		StringBuilder query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = null;
		SolrDocumentList results = null;
		int totalCount = 0;
		int readCount = 0;
		while(true){
			solrQuery.setStart(readCount);
			readCount +=100;
			solrQuery.setRows(100);
			queryResponse = client.query(solrQuery);
			results = queryResponse.getResults();
			for (SolrDocument solrDoc : results) {
				Object onlyId = solrDoc.get(EnumQuery.str_only_id.getCode());
				Object patientName = solrDoc.get(EnumQuery.str_patient_name.getCode());
				Object outHospitalDateTime = solrDoc.get(EnumQuery.date_out_hospital_date.getCode());
//				Object outHospitalType = solrDoc.get(EnumQuery.str_out_hospital_type.getCode());
				String outHospitalTypeName =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_out_hospital_type.getCode()));
				String inHospitalDayNumber =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_in_hospital_day_number.getCode()));
				String mainDiagCode =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_diag_code.getCode()));
				String mainDiag =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_diag.getCode()));
				Object totalCost =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_cost1.getCode()));
				Object cost22 =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_cost22.getCode()));
				Object cost13 =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_cost13.getCode()));
				String str_5 =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_5.getCode()));
				String str_5_diagname =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_5_diagname.getCode()));
				String int_last_in_hospital_interval=  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.int_last_in_hospital_interval.getCode()));
				Map<String, String> data = new HashMap<String, String>();
				data.put("only_id", ObjectUtils.getStringValue(onlyId));
				data.put("patient_name", ObjectUtils.getStringValue(patientName));
				data.put("out_hospital_date", ObjectUtils.getStringValue(outHospitalDateTime));
				data.put("str_out_hospital_type", ObjectUtils.getStringValue(outHospitalTypeName));
				data.put("float_cost1", totalCost+"");
				data.put("float_cost22", cost22+"");
				data.put("float_cost13", cost13+"");
				data.put("in_hospital_day_number", ObjectUtils.getStringValue(inHospitalDayNumber));
				data.put("str_5", str_5);
				data.put("int_last_in_hospital_interval", int_last_in_hospital_interval);
				data.put("str_5_diagname", str_5_diagname);
				data.put("main_hqms_diag_code", mainDiagCode);
				data.put("str_hqms_main_diag", mainDiag);
				if(int_last_in_hospital_interval != null){
					int reHospitalInterval = ObjectUtils.getIntegerValue(int_last_in_hospital_interval);
					if(reHospitalInterval==0){
						data.put("int_last_in_hospital_interval", "当天");
					}
					if(reHospitalInterval==2){
						data.put("int_last_in_hospital_interval", "2-15天");
					}
					if(reHospitalInterval==16){
						data.put("int_last_in_hospital_interval", "16-31天");
					}
					if(reHospitalInterval==32){
						data.put("int_last_in_hospital_interval", "大于31天");
					}
				}

				datas.add(data);
			}
			totalCount = (int)results.getNumFound();
			if(readCount >= totalCount){
				break;
			}

		}
		return datas;
	}

	@Override
	public List<Map<String, String>> unplanReOperateExportExcel(StatisticsAnalysisQuery params)
			throws SolrServerException, IOException {
		List<Map<String,String>> datas = new ArrayList<Map<String,String>>();
		String unplanReHospitalQuery = getUnplanReOperateQuery(params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		HttpSolrClient client = new  HttpSolrClient(myConfig.getSolrCorePath());
		StringBuilder queryField = new StringBuilder();
		queryField.append("visit_guid");
		queryField.append(","+EnumQuery.str_only_id);
		queryField.append(","+EnumQuery.str_patient_name);
		queryField.append(","+EnumQuery.str_mr_id);
		queryField.append(","+EnumQuery.int_visit_number);
		queryField.append(","+EnumQuery.date_birthday);
		queryField.append(","+EnumQuery.str_id_number);
		queryField.append(","+EnumQuery.str_out_dept);
		queryField.append(","+EnumQuery.date_out_hospital_date);
		queryField.append(","+EnumQuery.str_out_hospital_type);
		queryField.append(","+EnumQuery.float_cost1);
		queryField.append(","+EnumQuery.float_cost22);
		queryField.append(","+EnumQuery.float_cost13);
		queryField.append(","+EnumQuery.float_in_hospital_day_number);


		queryField.append(","+EnumQuery.str_hqms_is_un_planed_resurgery_operate_code);
		queryField.append(","+EnumQuery.str_hqms_is_un_planed_resurgery_operate_name);
		queryField.append(","+EnumQuery.str_hqms_is_un_planed_resurgery);
		queryField.append(","+EnumQuery.int_hqms_un_planed_resurgery_interval);
		queryField.append(","+EnumQuery.str_hqms_main_surgical_un_plan_reason);

		SolrQuery solrQuery = new SolrQuery();
		solrQuery.setFields(queryField.toString());
		StringBuilder query = new StringBuilder();
		query.append(unplanReHospitalQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = null;
		SolrDocumentList results = null;
		int totalCount = 0;
		int readCount = 0;
		while(true){
			solrQuery.setStart(readCount);
			readCount +=100;
			solrQuery.setRows(100);
			queryResponse = client.query(solrQuery);
			results = queryResponse.getResults();
			for (SolrDocument solrDoc : results) {
				Object onlyId = solrDoc.get(EnumQuery.str_only_id.getCode());
				String patientName = ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_patient_name.getCode()));
				String mrId = ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_mr_id.getCode()));
				Object visitNumber = solrDoc.get(EnumQuery.int_visit_number.getCode());
				Object birthday = solrDoc.get(EnumQuery.date_birthday.getCode());
				String idnumber = ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_id_number.getCode()));
				String outdept = ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_out_dept.getCode()));
				Object outHospitalDateTime = solrDoc.get(EnumQuery.date_out_hospital_date.getCode());
				String outHospitalTypeName =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_out_hospital_type.getCode()));
				String totalCost =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_cost1.getCode()));
				String cost22 =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_cost22.getCode()));
				String cost13 =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_cost13.getCode()));
				String inHospitalDayNumber =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_in_hospital_day_number.getCode()));

				String unplanedSurgeryCode =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_is_un_planed_resurgery_operate_code.getCode()));
				String unplanedSurgeryName =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_is_un_planed_resurgery_operate_name.getCode()));
				String unplanedSurgery =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_is_un_planed_resurgery.getCode()));
				String unplanedSurgeryInterval =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode()));
				String unplanedSurgeryreason =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_surgical_un_plan_reason.getCode()));


				Map<String, String> data = new HashMap<String, String>();
				data.put("only_id", ObjectUtils.getStringValue(onlyId));
				data.put("patient_name", ObjectUtils.getStringValue(patientName));
				data.put("mr_id", ObjectUtils.getStringValue(mrId));
				data.put("visit_number", ObjectUtils.getStringValue(visitNumber));
				data.put("birthday", ObjectUtils.getStringValue(birthday));
				data.put("id_number", ObjectUtils.getStringValue(idnumber));
				data.put("out_dept_name", ObjectUtils.getStringValue(outdept));
				data.put("out_hospital_date", ObjectUtils.getStringValue(outHospitalDateTime));
				data.put("out_hospital_type", ObjectUtils.getStringValue(outHospitalTypeName));
				data.put("cost1", totalCost+"");
				data.put("cost22", cost22+"");
				data.put("cost13", cost13+"");
				data.put("in_hospital_day_number", ObjectUtils.getStringValue(inHospitalDayNumber));
				data.put("un_planed_resurgery_operate_code", unplanedSurgeryCode);
				data.put("un_planed_resurgery_operate_name", unplanedSurgeryName);
				data.put("un_planed_resurgery", unplanedSurgery);
				data.put("main_surgical_un_plan_reason", unplanedSurgeryreason);
				if(unplanedSurgery != null){
					int unplanedSurgeryint = ObjectUtils.getIntegerValue(unplanedSurgery);
					if(unplanedSurgeryint==1){
						data.put("un_planed_resurgery", "是");
					}else{
						data.put("un_planed_resurgery", "否");
					}
				}
				if(unplanedSurgeryInterval != null){
					int reHospitalInterval = ObjectUtils.getIntegerValue(unplanedSurgeryInterval);
					if(reHospitalInterval==0){
						data.put("un_planed_resurgery_interval", "当天");
					}
					if(reHospitalInterval==2){
						data.put("un_planed_resurgery_interval", "2-15天");
					}
					if(reHospitalInterval==16){
						data.put("un_planed_resurgery_interval", "16-31天");
					}
					if(reHospitalInterval==32){
						data.put("un_planed_resurgery_interval", "大于31天");
					}
				}

				datas.add(data);
			}
			totalCount = (int)results.getNumFound();
			if(readCount >= totalCount){
				break;
			}

		}
		return datas;
	}

	@Override
	public List<Map<String, String>> sevenKeySurgeryForExport(StatisticsAnalysisQuery params)
			throws SolrServerException, IOException {
		// TODO Auto-generated method stub
		List<Map<String,String>> datas = new ArrayList<Map<String,String>>();
		String baseKeyDiseasesQuery = getBaseKeySurgeryQuery(params.getKeySurgeryCode(),params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		HttpSolrClient client = new  HttpSolrClient(myConfig.getSolrCorePath());
		StringBuilder queryField = new StringBuilder();
		queryField.append("visit_guid");
		queryField.append(","+EnumQuery.str_only_id);
		queryField.append(","+EnumQuery.str_patient_name);
		queryField.append(","+EnumQuery.date_out_hospital_date);
		queryField.append(","+EnumQuery.str_out_dept);
		queryField.append(","+EnumQuery.str_out_hospital_type);
		queryField.append(","+EnumQuery.float_in_hospital_day_number);
		queryField.append(","+EnumQuery.str_hqms_main_operate_code);
		queryField.append(","+EnumQuery.str_hqms_main_operate);
		queryField.append(","+EnumQuery.float_in_hospital_cost);
		queryField.append(","+EnumQuery.int_hqms_un_planed_resurgery_interval);
		SolrQuery solrQuery = new SolrQuery();
		solrQuery.setFields(queryField.toString());
		StringBuilder query = new StringBuilder();
		query.append(baseKeyDiseasesQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = null;
		SolrDocumentList results = null;
		int totalCount = 0;
		int readCount = 0;
		while(true){
			solrQuery.setStart(readCount);
			readCount +=100;
			solrQuery.setRows(100);
			queryResponse = client.query(solrQuery);
			results = queryResponse.getResults();
			for (SolrDocument solrDoc : results) {
				Object onlyId = solrDoc.get(EnumQuery.str_only_id.getCode());
				Object patientName = solrDoc.get(EnumQuery.str_patient_name.getCode());
				Object outHospitalDateTime = solrDoc.get(EnumQuery.date_out_hospital_date.getCode());
				Object outDeptName = solrDoc.get(EnumQuery.str_out_dept.getCode());
				String outHospitalTypeName =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_out_hospital_type.getCode()));
				String inHospitalDayNumber =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_in_hospital_day_number.getCode()));
				String mainOperateCode =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_operate_code.getCode()));
				String mainOperate =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_operate.getCode()));
				Object totalCost =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_in_hospital_cost.getCode()));
				Object unPlanedResurtyIntervalObj = solrDoc.get(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode());
				Map<String, String> data = new HashMap<String, String>();
				data.put("only_id", ObjectUtils.getStringValue(onlyId));
				data.put("patient_name", ObjectUtils.getStringValue(patientName));
				data.put("out_hospital_date", ObjectUtils.getStringValue(outHospitalDateTime));
				data.put("out_dept_name", ObjectUtils.getStringValue(outDeptName));
				if(outHospitalTypeName.contains("死")){
					data.put("is_death", "是");
				}else{
					data.put("is_death", "否");
				}
				data.put("in_hospital_day_number", ObjectUtils.getStringValue(inHospitalDayNumber));
				data.put("is_15_resurgery", "否");
				if(unPlanedResurtyIntervalObj != null){
					int unPlanedResurtyInterval = ObjectUtils.getIntegerValue(unPlanedResurtyIntervalObj);
					if(unPlanedResurtyInterval<15){
						data.put("is_15_resurgery", "是");
					}
				}
				data.put("main_surgery_code", mainOperateCode);
				data.put("main_surgery", mainOperate);

				data.put("total_cost", ObjectUtils.getStringValue(totalCost));
				datas.add(data);
			}
			totalCount = (int)results.getNumFound();
			if(readCount >= totalCount){
				break;
			}

		}



		return datas;
	}

	@Override
	public List<Map<String, String>> queryExportExcel(InPatientMedicalRecordQuery query,List<String> cols) {
		// TODO Auto-generated method stub

		List<Map<String, String>> records = new ArrayList<Map<String, String>>();
		HttpSolrClient client = null;

		try {
			client = new  HttpSolrClient(myConfig.getSolrCorePath());
			List<InPatientMedicalRecordItem> items = query.getItems();
			SolrQuery solrQuery = new SolrQuery();
			StringBuilder params = new StringBuilder();
			if(cols == null){
				cols = new ArrayList<String>();
			}

			cols.add(EnumQuery.str_patient_name.getCode());
			cols.add(EnumQuery.str_only_id.getCode());
			cols.add(EnumQuery.str_mr_id.getCode());
			cols.add(EnumQuery.int_visit_number.getCode());
			cols.add(EnumQuery.date_birthday.getCode());
			cols.add(EnumQuery.str_id_number.getCode());
			cols.add(EnumQuery.str_out_dept.getCode());
			cols.add(EnumQuery.date_out_hospital_date.getCode());
			cols.add(EnumQuery.str_out_hospital_type.getCode());
			cols.add(EnumQuery.float_in_hospital_cost.getCode());
			cols.add(EnumQuery.float_blood_cost.getCode());
			cols.add(EnumQuery.float_surgery_cost.getCode());
			cols.add(EnumQuery.float_in_hospital_day_number.getCode());
			for (InPatientMedicalRecordItem item : items) {
				if(!cols.contains(item.getQueryCode())){
					cols.add(item.getQueryCode());
				}
				if(item.getQueryCode().startsWith("str_") || item.getQueryCode().startsWith("array_") ) {
					if(item.getQueryCondition().equals("=")){
						String q =item.getQueryCode()+":"+item.getQueryValue();
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("!=")){
						String q ="-"+item.getQueryCode()+":"+item.getQueryValue();
/*						if(params.length() > 0){
							params.append(" "+item.getQueryCombination()+" ");
							params.append(q);
						}else{
							params.append(q);
						}*/
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals(">=")){
						String q = item.getQueryCode()+":["+item.getQueryValue()+" TO *]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals(">")){
						String q = item.getQueryCode()+":["+item.getQueryValue()+"! TO *]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}
					else if(item.getQueryCondition().equals("<=")){
						String q = item.getQueryCode()+":[* TO "+item.getQueryValue()+"]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("<")){
						String q = item.getQueryCode()+":[* TO "+item.getQueryValue()+"~]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("contains")){
						String q =item.getQueryCode()+":*"+item.getQueryValue()+"*";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("uncontains")){
						String q ="-"+item.getQueryCode()+":*"+item.getQueryValue()+"*";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("prefix")){
						String q =item.getQueryCode()+":"+item.getQueryValue()+"*";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals("suffix")){
						String q =item.getQueryCode()+":*"+item.getQueryValue();
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}
				}else if(item.getQueryCode().startsWith("date_")) {

					if(item.getQueryCondition().equals("=") || item.getQueryCondition().equals("contains")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							Date endDate = DateUtil.getNextDay(startDate);
							String q = item.getQueryCode()+":["+DateUtil.dateFormat(solrDateFormat, startDate)+" TO "+DateUtil.dateFormat(solrDateFormat, endDate)+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals(">=")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":["+DateUtil.dateFormat(solrDateFormat, startDate)+" TO *]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals(">")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":["+DateUtil.dateFormat(solrDateFormat, startDate)+" TO *]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else if(item.getQueryCondition().equals("<=")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":[* TO "+DateUtil.dateFormat(solrDateFormat, DateUtil.getNextDay(startDate))+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals("<")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":[* TO "+DateUtil.dateFormat(solrDateFormat, DateUtil.getNextDay(startDate))+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}


					/*
					if(item.getQueryCondition().equals("=") || item.getQueryCondition().equals("contains")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							Date endDate = DateUtil.getNextDay(startDate);
							String q = item.getQueryCode()+":["+DateUtil.dateFormat(solrDateFormat, startDate)+" TO "+DateUtil.dateFormat(solrDateFormat, endDate)+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals(">=")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":["+DateUtil.dateFormat(solrDateFormat, startDate)+" TO *]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals(">")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":["+DateUtil.dateFormat(solrDateFormat, DateUtil.getNextDay(startDate))+" TO *]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else if(item.getQueryCondition().equals("<=")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":[* TO "+DateUtil.dateFormat(solrDateFormat, startDate)+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals("<")){
						try {
							Date startDate = DateUtil.dateParse(item.getQueryValue());
							String q = item.getQueryCode()+":[* TO "+DateUtil.dateFormat(solrDateFormat, DateUtil.getLastDay(startDate))+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				*/}else if(item.getQueryCode().startsWith("int_") || item.getQueryCode().startsWith("float_")) {

					if(item.getQueryCondition().equals("=") || item.getQueryCondition().equals("contains")){

						String q = item.getQueryCode()+":["+item.getQueryValue()+" TO "+item.getQueryValue()+"]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals(">=")){
						String q = item.getQueryCode()+":["+item.getQueryValue()+" TO *]";
						addQueryCombination(params,item.getQueryCombination());
						params.append(q);
					}else if(item.getQueryCondition().equals(">")){
						try {
							int number = Integer.parseInt(item.getQueryValue())+1;
							String q = item.getQueryCode()+":["+number+" TO *]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals("<=")){
						try {
							int number = Integer.parseInt(item.getQueryValue())+1;
							String q = item.getQueryCode()+":[* TO "+number+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else if(item.getQueryCondition().equals("<")){
						try {
							int number = Integer.parseInt(item.getQueryValue())-1;
							String q = item.getQueryCode()+":[* TO "+number+"]";
							addQueryCombination(params,item.getQueryCombination());
							params.append(q);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}


				}
			}
			params.append(")");
			solrQuery.setQuery(params.toString());
			StringBuilder queryField = new StringBuilder();
			queryField.append("visit_guid");
			for (String col : cols) {
				queryField.append(","+col);
			}

			solrQuery.setFields(queryField.toString());
			if(query.getPageSize() != null){
				solrQuery.setRows(query.getPageSize());
			}else{
				solrQuery.setRows(Integer.MAX_VALUE);
			}

			solrQuery.setStart(query.getCurrentStartNum());
			QueryResponse queryResponse = client.query(solrQuery);
			SolrDocumentList results = queryResponse.getResults();
			query.setTotalCounts((int)results.getNumFound());
			for (SolrDocument solrDoc : results) {
				Map<String, String> record = new HashMap<String, String>();
				for (String col : cols) {

//					Object visitGuid = solrDoc.get("visit_guid");
//					Object patientName = solrDoc.get(EnumQuery.str_patient_name.getCode());
//					Object onlyId = solrDoc.get(EnumQuery.str_only_id.getCode());
//					Object mrId = solrDoc.get(EnumQuery.str_mr_id.getCode());
//					Object visitNumber = solrDoc.get(EnumQuery.int_visit_number.getCode());
//					Object idNumber = solrDoc.get(EnumQuery.str_id_number.getCode());
//					Object outDeptName = solrDoc.get(EnumQuery.str_out_dept.getCode());
//					Object outHospitalTypeName = solrDoc.get(EnumQuery.str_out_hospital_type.getCode());
//					Object birthday = solrDoc.get(EnumQuery.date_birthday.getCode());
//					Object outHospitalDateTime = solrDoc.get(EnumQuery.date_out_hospital_date.getCode());
//					Object inHospitalCost = solrDoc.get(EnumQuery.float_in_hospital_cost.getCode());
//					Object bloodCost = solrDoc.get(EnumQuery.float_blood_cost.getCode());
//					Object surgeryCost = solrDoc.get(EnumQuery.float_surgery_cost.getCode());
//					Object inHospitalDayNumber = solrDoc.get(EnumQuery.float_in_hospital_day_number.getCode());
//
//					MedicalRecordSolr medicalRecordSolr = new MedicalRecordSolr();
//					medicalRecordSolr.setVisitGuid(ObjectUtils.getStringValue(visitGuid));
//					medicalRecordSolr.setOnlyId(ObjectUtils.getStringValue(onlyId));
//					medicalRecordSolr.setMrId(ObjectUtils.getStringValue(mrId));
//					medicalRecordSolr.setVisitNumber(ObjectUtils.getIntegerValue(visitNumber));
//					medicalRecordSolr.setIdNumber(ObjectUtils.getStringValue(idNumber));
//
//					medicalRecordSolr.setPatientName(ObjectUtils.getStringValue(patientName));
//
//					medicalRecordSolr.setBirthday(ObjectUtils.getDateTimeValue(birthday));
//					medicalRecordSolr.setOutDeptName(ObjectUtils.getStringValue(outDeptName));
//					medicalRecordSolr.setOutHospitalTypeName(ObjectUtils.getStringValue(outHospitalTypeName));
//					medicalRecordSolr.setBirthday(ObjectUtils.getDateValue(birthday));
//					medicalRecordSolr.setOutHospitalDateTime(ObjectUtils.getGTCDeteTimeValue(outHospitalDateTime));
//					medicalRecordSolr.setInHospitalDayNumber(ObjectUtils.getIntegerValue(inHospitalDayNumber));
//					medicalRecordSolr.setTotalCostMoney(ObjectUtils.getBigDecimalValue(inHospitalCost));
//					medicalRecordSolr.setBloodCost(ObjectUtils.getBigDecimalValue(bloodCost));
//					medicalRecordSolr.setSurgeryCost(ObjectUtils.getBigDecimalValue(surgeryCost));
					if(EnumQuery.date_out_hospital_date.getCode().equals(col)) {
						Object outHospitalDateTimeStr =solrDoc.get(col);
						Date outHospitalDateTimeDate = ObjectUtils.getGTCDeteTimeValue(outHospitalDateTimeStr);
						record.put(col, DateUtil.dateFormat("yyyy-MM-dd",outHospitalDateTimeDate));
					}else {
						record.put(col, ObjectUtils.getStringValue(solrDoc.get(col)));
					}


				}
				records.add(record);
			}
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(client!= null){
					client.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return records;


	}


	private String getTertiaryHospitalKeySurgeryQuery(String keySurgeryCode,Date outHospitalStartDate,Date outHospitalEndDate){
		StringBuilder query = new StringBuilder();
		query.append(EnumQuery.date_out_hospital_date+":["+DateUtil.dateFormat(solrDateFormat, outHospitalStartDate)+" TO "+DateUtil.dateFormat(solrDateFormat, outHospitalEndDate)+"]");
		query.append(" AND ");
		query.append(EnumQuery.int_age_year.getCode()+":[18 TO *]");
		if(keySurgeryCode.equalsIgnoreCase("1")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[81.51! TO 81.55~]");
		}else if(keySurgeryCode.equalsIgnoreCase("2")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[03.01! TO 03.09~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[03.40! TO 03.79~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[80.51! TO 80.59~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[81.01! TO 81.38~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[81.62! TO 81.66~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[84.61! TO 84.68~]");
			query.append(" ) ");

		}else if(keySurgeryCode.equalsIgnoreCase("3")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[52.51! TO 52.96~]");
		}else if(keySurgeryCode.equalsIgnoreCase("4")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[42.41! TO 42.65~]");
		}else if(keySurgeryCode.equalsIgnoreCase("5")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":51.23*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":51.24*");
			query.append(" ) ");

		}else if(keySurgeryCode.equalsIgnoreCase("6")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":36.1*");
		}else if(keySurgeryCode.equalsIgnoreCase("7")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":00.66*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":36.06*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":36.07*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("8")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[01.21! TO 01.59~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[02.01! TO 02.99~]");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("9")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[68.3! TO 68.7~]");
		}else if(keySurgeryCode.equalsIgnoreCase("10")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[74.0! TO 74.2~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":74.4*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":74.9*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("11")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":O80*");
			query.append(" OR ");
			query.append(EnumQuery.array_hqms_other_diag_codes.getCode()+":O80*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("12")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":85.4*");
		}else if(keySurgeryCode.equalsIgnoreCase("13")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[32.20! TO 32.60~]");
		}else if(keySurgeryCode.equalsIgnoreCase("14")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[43.50! TO 43.99~]");
		}else if(keySurgeryCode.equalsIgnoreCase("15")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[48.40! TO 48.69~]");
		}else if(keySurgeryCode.equalsIgnoreCase("16")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[55.40! TO 55.69~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[60.21! TO 60.69~]");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("17")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[38.02! TO 38.18~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[38.30! TO 38.89~]");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[39.00! TO 39.59~]");
			query.append(" ) ");
		}
		return query.toString();
	}

	@Override
	public List<Map<String, Object>> tertiaryHospitalKeySurgery(StatisticsAnalysisQuery params)
			throws SolrServerException, IOException {
		// TODO Auto-generated method stub
		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		HttpSolrClient client = null;
		String baseKeySurgeryQuery = getTertiaryHospitalKeySurgeryQuery(params.getKeySurgeryCode(),params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		client = new  HttpSolrClient(myConfig.getSolrCorePath());
		SolrQuery solrQuery = new SolrQuery();
		StringBuilder query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = client.query(solrQuery);
		SolrDocumentList results = queryResponse.getResults();
		int surgeryCount = (int)results.getNumFound();
		data.put("surgery_count",surgeryCount);
		query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		query.append(" AND ");
		query.append(EnumQuery.str_out_hospital_type+":死亡");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		data.put("death_count", (int)results.getNumFound());
		query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode()+":[0 TO 15]");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate15Count = (int)results.getNumFound();
		data.put("count_15", rate15Count);
		data.put("rate_15", NumberUtils.getPrecent(new BigDecimal(rate15Count), new BigDecimal(surgeryCount))+"%");

		query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		solrQuery.setGetFieldStatistics(true);
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_day_number.getCode());
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_cost.getCode());
		queryResponse = client.query(solrQuery);
		Map<String,FieldStatsInfo> fieldStatsInfos = queryResponse.getFieldStatsInfo();
		if(fieldStatsInfos != null){
			for (String fieldKey : fieldStatsInfos.keySet()) {
				if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_cost.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalCost = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());

					data.put("avg_in_hospital_cost",avgInHospitalCost==null?null:NumberUtils.getString(avgInHospitalCost, 2));

				}else if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_day_number.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalDay = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());

					data.put("avg_in_hospital_day",avgInHospitalDay==null?null:NumberUtils.getString(avgInHospitalDay, 2));

				}
			}
		}
		datas.add(data);
		return datas;

	}

	@Override
	public List<Map<String, String>> tertiaryHospitalKeySurgeryForExport(StatisticsAnalysisQuery params)
			throws SolrServerException, IOException {
		// TODO Auto-generated method stub
		List<Map<String,String>> datas = new ArrayList<Map<String,String>>();
		String baseKeyDiseasesQuery = getTertiaryHospitalKeySurgeryQuery(params.getKeySurgeryCode(),params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		HttpSolrClient client = new  HttpSolrClient(myConfig.getSolrCorePath());
		StringBuilder queryField = new StringBuilder();
		queryField.append("visit_guid");
		queryField.append(","+EnumQuery.str_only_id);
		queryField.append(","+EnumQuery.str_patient_name);
		queryField.append(","+EnumQuery.date_out_hospital_date);
		queryField.append(","+EnumQuery.str_out_dept);
		queryField.append(","+EnumQuery.str_out_hospital_type);
		queryField.append(","+EnumQuery.float_in_hospital_day_number);
		queryField.append(","+EnumQuery.str_hqms_main_operate_code);
		queryField.append(","+EnumQuery.str_hqms_main_operate);
		queryField.append(","+EnumQuery.float_in_hospital_cost);
		queryField.append(","+EnumQuery.int_hqms_un_planed_resurgery_interval);
		SolrQuery solrQuery = new SolrQuery();
		solrQuery.setFields(queryField.toString());
		StringBuilder query = new StringBuilder();
		query.append(baseKeyDiseasesQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = null;
		SolrDocumentList results = null;
		int totalCount = 0;
		int readCount = 0;
		while(true){
			solrQuery.setStart(readCount);
			readCount +=100;
			solrQuery.setRows(100);
			queryResponse = client.query(solrQuery);
			results = queryResponse.getResults();
			for (SolrDocument solrDoc : results) {
				Object onlyId = solrDoc.get(EnumQuery.str_only_id.getCode());
				Object patientName = solrDoc.get(EnumQuery.str_patient_name.getCode());
				Object outHospitalDateTime = solrDoc.get(EnumQuery.date_out_hospital_date.getCode());
				Object outDeptName = solrDoc.get(EnumQuery.str_out_dept.getCode());
				String outHospitalTypeName =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_out_hospital_type.getCode()));
				String inHospitalDayNumber =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_in_hospital_day_number.getCode()));
				String mainOperateCode =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_operate_code.getCode()));
				String mainOperate =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_operate.getCode()));
				Object totalCost =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_in_hospital_cost.getCode()));
				Object unPlanedResurtyIntervalObj = solrDoc.get(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode());
				Map<String, String> data = new HashMap<String, String>();
				data.put("only_id", ObjectUtils.getStringValue(onlyId));
				data.put("patient_name", ObjectUtils.getStringValue(patientName));
				data.put("out_hospital_date", ObjectUtils.getStringValue(outHospitalDateTime));
				data.put("out_dept_name", ObjectUtils.getStringValue(outDeptName));
				if(outHospitalTypeName.contains("死")){
					data.put("is_death", "是");
				}else{
					data.put("is_death", "否");
				}
				data.put("in_hospital_day_number", ObjectUtils.getStringValue(inHospitalDayNumber));
				data.put("is_15_resurgery", "否");
				if(unPlanedResurtyIntervalObj != null){
					int unPlanedResurtyInterval = ObjectUtils.getIntegerValue(unPlanedResurtyIntervalObj);
					if(unPlanedResurtyInterval<15){
						data.put("is_15_resurgery", "是");
					}
				}
				data.put("main_surgery_code", mainOperateCode);
				data.put("main_surgery", mainOperate);

				data.put("total_cost", ObjectUtils.getStringValue(totalCost));
				datas.add(data);
			}
			totalCount = (int)results.getNumFound();
			if(readCount >= totalCount){
				break;
			}

		}



		return datas;

	}


	private String getTertiaryHospitalMalignantTumoryQuery(String keySurgeryCode,Date outHospitalStartDate,Date outHospitalEndDate){
		StringBuilder query = new StringBuilder();
		query.append(EnumQuery.date_out_hospital_date+":["+DateUtil.dateFormat(solrDateFormat, outHospitalStartDate)+" TO "+DateUtil.dateFormat(solrDateFormat, outHospitalEndDate)+"]");
		query.append(" AND ");
		query.append(EnumQuery.int_age_year.getCode()+":[18 TO *]");
		if(keySurgeryCode.equalsIgnoreCase("1")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C73*");
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":[06.2! TO 06.5~]");
		}else if(keySurgeryCode.equalsIgnoreCase("2")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C32*");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":30.3*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":30.4*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("3")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C34*");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":32.4*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":32.5*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":32.6*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("4")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C15*");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":42.5*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":42.6*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("5")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C16*");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":43.5*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":43.6*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":43.7*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":43.9*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("6")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C22*");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":50.2*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":50.3*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":50.4*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":50.5*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("7")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":[C18! TO C20~]");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":45.7*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":48.4*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":48.5*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":48.6*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("8")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C25*");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":52.5*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":52.7*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("9")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C50*");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":85.4*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":85.21*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("10")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C64*");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":55.3*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":55.5*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("11")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C61*");
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":60.5*");
		}else if(keySurgeryCode.equalsIgnoreCase("12")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C67*");
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":57.7*");
		}else if(keySurgeryCode.equalsIgnoreCase("13")){
			query.append(" AND ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C56*");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":65.6*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":40.59*");
			query.append(" ) ");
		}else if(keySurgeryCode.equalsIgnoreCase("14")){
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":C53*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_diag_code.getCode()+":D06*");
			query.append(" ) ");
			query.append(" AND ");
			query.append(" ( ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":40.59*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":65.6*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":67.2*");
			query.append(" OR ");
			query.append(EnumQuery.str_hqms_main_operate_code.getCode()+":68.4*");
			query.append(" ) ");

		}
		return query.toString();
	}

	@Override
	public List<Map<String, Object>> tertiaryHospitalMalignantTumor(StatisticsAnalysisQuery params)
			throws SolrServerException, IOException {

		// TODO Auto-generated method stub
		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		HttpSolrClient client = null;
		String baseKeySurgeryQuery = getTertiaryHospitalMalignantTumoryQuery(params.getKeySurgeryCode(),params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		client = new  HttpSolrClient(myConfig.getSolrCorePath());
		SolrQuery solrQuery = new SolrQuery();
		StringBuilder query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = client.query(solrQuery);
		SolrDocumentList results = queryResponse.getResults();
		int surgeryCount = (int)results.getNumFound();
		data.put("surgery_count",surgeryCount);
		query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		query.append(" AND ");
		query.append(EnumQuery.str_out_hospital_type+":死亡");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		data.put("death_count", (int)results.getNumFound());
		query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		query.append(" AND ");
		query.append(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode()+":[0 TO 15]");
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		queryResponse = client.query(solrQuery);
		results = queryResponse.getResults();
		int rate15Count = (int)results.getNumFound();
		data.put("count_15", rate15Count);
		data.put("rate_15", NumberUtils.getPrecent(new BigDecimal(rate15Count), new BigDecimal(surgeryCount))+"%");

		query = new StringBuilder();
		query.append(baseKeySurgeryQuery);
		solrQuery.clear();
		solrQuery.setQuery(query.toString());
		solrQuery.setGetFieldStatistics(true);
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_day_number.getCode());
		solrQuery.setGetFieldStatistics(EnumQuery.float_in_hospital_cost.getCode());
		queryResponse = client.query(solrQuery);
		Map<String,FieldStatsInfo> fieldStatsInfos = queryResponse.getFieldStatsInfo();
		if(fieldStatsInfos != null){
			for (String fieldKey : fieldStatsInfos.keySet()) {
				if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_cost.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalCost = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());

					data.put("avg_in_hospital_cost",avgInHospitalCost==null?null:NumberUtils.getString(avgInHospitalCost, 2));

				}else if(fieldKey != null && fieldKey.equalsIgnoreCase(EnumQuery.float_in_hospital_day_number.getCode())){
					FieldStatsInfo fieldStatsInfo = fieldStatsInfos.get(fieldKey);
					BigDecimal avgInHospitalDay = ObjectUtils.getBigDecimalValue(fieldStatsInfo.getMean());

					data.put("avg_in_hospital_day",avgInHospitalDay==null?null:NumberUtils.getString(avgInHospitalDay, 2));

				}
			}
		}
		datas.add(data);
		return datas;


	}

	@Override
	public List<Map<String, String>> tertiaryHospitalMalignantTumorForExport(StatisticsAnalysisQuery params)
			throws SolrServerException, IOException {

		// TODO Auto-generated method stub
		List<Map<String,String>> datas = new ArrayList<Map<String,String>>();
		String baseKeyDiseasesQuery = getTertiaryHospitalMalignantTumoryQuery(params.getKeySurgeryCode(),params.getOutHospitalStartDate(),params.getOutHospitalEndDate());
		HttpSolrClient client = new  HttpSolrClient(myConfig.getSolrCorePath());
		StringBuilder queryField = new StringBuilder();
		queryField.append("visit_guid");
		queryField.append(","+EnumQuery.str_only_id);
		queryField.append(","+EnumQuery.str_patient_name);
		queryField.append(","+EnumQuery.date_out_hospital_date);
		queryField.append(","+EnumQuery.str_out_dept);
		queryField.append(","+EnumQuery.str_out_hospital_type);
		queryField.append(","+EnumQuery.float_in_hospital_day_number);
		queryField.append(","+EnumQuery.str_hqms_main_diag_code);
		queryField.append(","+EnumQuery.str_hqms_main_diag);
		queryField.append(","+EnumQuery.str_hqms_main_operate_code);
		queryField.append(","+EnumQuery.str_hqms_main_operate);
		queryField.append(","+EnumQuery.float_in_hospital_cost);
		queryField.append(","+EnumQuery.int_hqms_un_planed_resurgery_interval);
		SolrQuery solrQuery = new SolrQuery();
		solrQuery.setFields(queryField.toString());
		StringBuilder query = new StringBuilder();
		query.append(baseKeyDiseasesQuery);
		solrQuery.setQuery(query.toString());
		QueryResponse queryResponse = null;
		SolrDocumentList results = null;
		int totalCount = 0;
		int readCount = 0;
		while(true){
			solrQuery.setStart(readCount);
			readCount +=100;
			solrQuery.setRows(100);
			queryResponse = client.query(solrQuery);
			results = queryResponse.getResults();
			for (SolrDocument solrDoc : results) {
				Object onlyId = solrDoc.get(EnumQuery.str_only_id.getCode());
				Object patientName = solrDoc.get(EnumQuery.str_patient_name.getCode());
				Object outHospitalDateTime = solrDoc.get(EnumQuery.date_out_hospital_date.getCode());
				Object outDeptName = solrDoc.get(EnumQuery.str_out_dept.getCode());
				String outHospitalTypeName =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_out_hospital_type.getCode()));
				String inHospitalDayNumber =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_in_hospital_day_number.getCode()));
				String mainDiagCode =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_diag_code.getCode()));
				String mainDiag =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_diag.getCode()));

				String mainOperateCode =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_operate_code.getCode()));
				String mainOperate =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.str_hqms_main_operate.getCode()));
				Object totalCost =  ObjectUtils.getStringValue(solrDoc.get(EnumQuery.float_in_hospital_cost.getCode()));
				Object unPlanedResurtyIntervalObj = solrDoc.get(EnumQuery.int_hqms_un_planed_resurgery_interval.getCode());
				Map<String, String> data = new HashMap<String, String>();
				data.put("only_id", ObjectUtils.getStringValue(onlyId));
				data.put("patient_name", ObjectUtils.getStringValue(patientName));
				data.put("out_hospital_date", ObjectUtils.getStringValue(outHospitalDateTime));
				data.put("out_dept_name", ObjectUtils.getStringValue(outDeptName));
				if(outHospitalTypeName.contains("死")){
					data.put("is_death", "是");
				}else{
					data.put("is_death", "否");
				}
				data.put("in_hospital_day_number", ObjectUtils.getStringValue(inHospitalDayNumber));
				data.put("is_15_resurgery", "否");
				if(unPlanedResurtyIntervalObj != null){
					int unPlanedResurtyInterval = ObjectUtils.getIntegerValue(unPlanedResurtyIntervalObj);
					if(unPlanedResurtyInterval<15){
						data.put("is_15_resurgery", "是");
					}
				}
				data.put("main_diag_code", mainDiagCode);
				data.put("main_diag", mainDiag);
				data.put("main_surgery_code", mainOperateCode);
				data.put("main_surgery", mainOperate);

				data.put("total_cost", ObjectUtils.getStringValue(totalCost));
				datas.add(data);
			}
			totalCount = (int)results.getNumFound();
			if(readCount >= totalCount){
				break;
			}

		}

		return datas;


	}

}
